package basic.study.wantOffer.chapter3;

import java.util.Stack;

/**
 * @ClassName Problem31
 * @Description 栈的压入、弹出序列
 * @Company inspur
 * @Author Kevin
 * @Date 2020/5/19 11:58
 * @Version 1.0
 */
public class Problem31 {
    class StackPushPopOrder {
        public boolean isPopOrder(int[] pushA, int[] popA) {
            Stack<Integer> stack = new Stack<Integer>();
            int popIndex = 0;
            if (pushA == null || popA == null) {
                return false;
            }
            if (pushA.length != popA.length || pushA.length == 0) {
                return false;
            }
            for (int pushIndex = 0; pushIndex < pushA.length; pushIndex++) {
                stack.push(pushA[pushIndex]);
                while (!stack.empty() && stack.peek() == popA[popIndex]) {
                    stack.pop();
                    popIndex++;
                }
            }
            return stack.empty();
        }
    }
}
